package com.team7.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team7.common.entity.ShoucangCourse;
import com.team7.dao.ShoucangCourseDao;
import com.team7.service.ShoucangCourseService;
import com.team7.vo.CommonResult;
import com.team7.vo.FavoriteUtil;
import com.team7.vo.FavoriteVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: 1026
 * @description:
 * @author: 田宏洋
 * @create: 2021-10-26 15:01
 **/
@Service
public class ShouChangCourseImpl extends ServiceImpl<ShoucangCourseDao, ShoucangCourse> implements ShoucangCourseService {

    @Resource
    private ShoucangCourseDao shoucangCourseDao;
    @Override
    public CommonResult addFavoritecourse(FavoriteVo favoriteVo) {
        QueryWrapper<ShoucangCourse> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("member_id",favoriteVo.getMemberid());
        objectQueryWrapper.eq("course_id",favoriteVo.getCourseid());
        ShoucangCourse shoucangCourse = shoucangCourseDao.selectOne(objectQueryWrapper);
        if(shoucangCourse==null){
            ShoucangCourse shoucangCourse1 = new ShoucangCourse();
            shoucangCourse1.setCourseId(favoriteVo.getCourseid());
            shoucangCourse1.setMemberId(favoriteVo.getMemberid());
            int insert = shoucangCourseDao.insert(shoucangCourse1);
            if(insert>0){
                return new CommonResult(2000,"收藏成功",null);
            }

        }
        return new CommonResult(5000,"已添加收藏",null);
    }

    @Override
    public CommonResult findFavoriteCourse(FavoriteVo favoriteVo) {
        QueryWrapper<ShoucangCourse> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("member_id",favoriteVo.getMemberid());
        objectQueryWrapper.eq("course_id",favoriteVo.getCourseid());
        ShoucangCourse shoucangCourse = shoucangCourseDao.selectOne(objectQueryWrapper);
        if(shoucangCourse==null){
            favoriteVo.setStatus(1);
            return new CommonResult(2000,"查询成功",favoriteVo);
        }else{
            favoriteVo.setStatus(0);
            return new CommonResult(2000,"查询成功",favoriteVo);
        }
    }

    @Override
    public CommonResult deleteFavoriteCourse(FavoriteVo favoriteVo) {
        QueryWrapper<ShoucangCourse> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("member_id",favoriteVo.getMemberid());
        objectQueryWrapper.eq("course_id",favoriteVo.getCourseid());
        int shoucangCourse = shoucangCourseDao.delete(objectQueryWrapper);
        if(shoucangCourse>0){
            return new CommonResult(2000,"成功",null);
        }else{

            return new CommonResult(5000,"失败",null);
        }
    }

    @Override
    public CommonResult findFavoriteCourseById(String memberid) {
        List<FavoriteUtil> favoriteCourseById = shoucangCourseDao.findFavoriteCourseById(memberid);
        if(favoriteCourseById.size()>0){
            return new CommonResult(2000,"查询成功",favoriteCourseById);
        }
        return new CommonResult(5000,"暂无收藏",favoriteCourseById);
    }
}
